package cc.jq1024.middleware.whitelist.test.interfaces;

import cc.jq1024.middleware.token.service.ITokenService;
import cc.jq1024.middleware.whitelist.annotation.DoWhiteList;
import cc.jq1024.middleware.whitelist.valobj.WhiteListType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;


@RestController
public class UserController {

    @Resource
    private ITokenService tokenService;

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 通过：http://localhost:8081/api/queryUserInfo?userId=aaa
     * 拦截：http://localhost:8081/api/queryUserInfo?userId=123
     */
    @DoWhiteList(type = WhiteListType.PARAMS_TYPE, paramKey = "userId")
    @RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET)
    public UserInfo queryUserInfo(@RequestParam("userId") String userId) {
        logger.info("查询用户信息，userId：{}", userId);
        return new UserInfo("虫虫:" + userId, 19, "天津市东丽区万科赏溪苑14-0000");
    }


    /**
     * 通过：http://localhost:8081/api/queryUserInfoToken?authorization=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjY2ZAbGlqaWFxaWFuZyIsImV4cCI6MTcyNTk5MDM0OSwidXNlcklkIjoiMTExIiwiaWF0IjoxNzI1OTQ3MTQ5LCJqdGkiOiI5NTAxODZlYi0wNTE2LTQxMzAtYTI0OC0zN2NlNDhkYWUzZTUifQ.OAeuzcYJR-z45MWSYCWV1iGkv9oeI2ieRsp1qDtMVUA
     */
    @DoWhiteList(type = WhiteListType.TOKEN_TYPE, tokenKey = "Authorization")
    @RequestMapping(path = "/api/queryUserInfoToken", method = RequestMethod.GET)
    public UserInfo queryUserInfoToken(@RequestParam("Authorization") String authorization) {
        return new UserInfo("虫虫:45", 19, "天津市东丽区万科赏溪苑14-0000");
    }

    /**
     * 通过：http://localhost:8081/api/initToken?userId=111
     */
    @RequestMapping(path = "/api/initToken", method = RequestMethod.GET)
    public UserInfo initToken(@RequestParam("userId") String userId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        String jwtToken = tokenService.createJWT(map);
        logger.info("token：{}", jwtToken);
        return new UserInfo("虫虫:" + userId, 19, "天津市东丽区万科赏溪苑14-0000");
    }

}

